// stdx/convertx.rs
// CRITICAL: From/Into/TryFrom/TryInto 与 AsRef/AsMut 示例
// AUTO_COMMENT: 展示类型转换习惯用法

#[allow(dead_code)]
pub fn stdx_examples_convertx() {
    // CRITICAL: From/Into 示例
    let s = String::from("hello");
    let _s2: String = s.clone().into();

    // CRITICAL: TryFrom/TryInto 示例（用 Result 表示失败）
    let r: Result<i32, _> = i32::try_from(10i64);

    // CRITICAL: AsRef/AsMut 示例
    fn needs_ref<S: AsRef<str>>(s: S) -> usize {
        s.as_ref().len()
    }
    let l = needs_ref("hi");

    let _ = (r, l);

    // CRITICAL: 自定义 From 实现
    #[derive(Debug)]
    struct MyStruct {
        value: i32,
    }

    impl From<i32> for MyStruct {
        fn from(value: i32) -> Self {
            MyStruct { value }
        }
    }

    let ms: MyStruct = 42.into();

    // CRITICAL: TryInto 示例
    let big_num: i64 = 1000;
    let small_num: Result<i32, _> = big_num.try_into();

    // CRITICAL: AsRef 用于借用转换
    let vec = vec![1, 2, 3];
    let slice: &[i32] = vec.as_ref();

    // CRITICAL: AsMut 用于可变借用转换
    let mut string = String::from("hello");
    let mut_str: &mut str = string.as_mut();

    // CRITICAL: ToOwned 示例（从借用创建拥有类型）
    let borrowed: &str = "world";
    let owned: String = borrowed.to_owned();

    let _ = (ms, small_num, slice, mut_str, owned);
}
